#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
        int Month=0;
        int Y=0;        
        int y =0; 
        int day = 0;
        int Firstday; 
        int k = 0;
        int i = 0;
        int m = 0;
        int tday;
        int dmonth;
        while( Y < 1 || Y > 10000)
        {
           cout<<"input year";
           cin>>Y;
        }
        while(Month < 1 || Month > 12)
        {
           
        	cout<<"input month";
        	cin>>Month;
        }

        dmonth = 30 * (Month == 4 || Month == 6 || Month == 9 || Month == 11)
                + 31 * (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)               
                + (28 + (Y % 400 == 0 || (Y % 4 == 0 && Y % 100 != 0))) * (Month == 2);   
                
        y=1;
        while(y<Y)
        {       y=y+1;          
                k = k + (y % 400 == 0 || y % 4 == 0 && y % 100 != 0);
        }
        tday = k * 366 + (Y - k - 1) * 365;
              
        m=1;
        while(m < Month)
        {
                tday = tday 
                + 30 * (m == 4 || m == 6 || m == 9 || m == 11)
                + 31 * (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)           
                + (28 + (Y % 400 == 0 || (Y % 4 == 0 && Y % 100 != 0))) * (m == 2);
				m = m + 1;
        }
        
          
        Firstday = (1 + (tday % 7)) % 7; 
        y = Firstday;
		cout<<"\nSu  Mo  Tu  We  Th  Fr  Sa\n";
		cout<< "----------------------------\n";
		
		i=0;
	    while(i<Firstday)
	        { 
				cout<<"    ";
		        i = i + 1;
	        }	       
			
		day =1;
		y = y+1;
	    while(day <= dmonth)
        {  			
	        if(day<=9){
	        	cout<<" ";
	        }
	        cout<<day;
		    cout<<"  ";
		    
	        if(y>=7)
	        {
	        	 cout<<"\n";
                 y = 0;
	        }
	        y = y + 1;
	        day = day + 1;	            
        }
        //PutStr("\n");
        cout<<"\n";
        

}
